﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThreadForm
{
    public partial class Form1 : Form
    {
        Thread fillListThread;
        CTextOutput CTextOutput;
        public Form1()
        {
            InitializeComponent();
            CTextOutput = new CTextOutput(this, "default");
        }

        // 定义一个添加文字的委托
        private delegate void AddTextDelegate(string text);

        public void AddText(string text)
        {
            if (listBox1.InvokeRequired)
            {
                AddTextDelegate textDelegate = AddText;

                listBox1.Invoke(textDelegate, text);
            }
            else
            {
                listBox1.Items.Add(text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            CTextOutput.shouldstop = false;
            CTextOutput.strOutputText = textBox1.Text;
            // 线程开启,CTextOutput 调用 addText()添加输入
            fillListThread = new Thread(CTextOutput.WriteText);
            fillListThread.IsBackground = true;
            fillListThread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CTextOutput.shouldstop = true;

            fillListThread.Join(100);

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // 文字风声变化立即赋值
            CTextOutput.strOutputText = textBox1.Text;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {

        }

        private void uiPagination1_Click(object sender, EventArgs e)
        {

        }
    }
}
